//
//  ExampleViewController.m
//  SegmentView
//
//  Created by tom.sun on 16/5/27.
//  Copyright © 2016年 tom.sun. All rights reserved.
//

#import "ExampleViewController.h"
#import "UIView+frame.h"
#import "NoticeCell.h"
#import "NoticeList.h"
#import "DetailNoticeViewController.h"


@interface ExampleViewController ()<UITableViewDelegate,UITableViewDataSource>

//定义tableView
@property (nonatomic, strong) UITableView *tableView;

//公告详情标题
@property (nonatomic, copy) NSString *titleStr;

//数据原数组
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ExampleViewController
- (instancetype)initWithIndex:(NSInteger)index title:(NSString *)title {
    self = [super init];
    if (self) {
        NSLog(@"%ld",index);
        _titleStr = title;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [[NSMutableArray alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView:) name:@"ChangeNameNotification" object:nil];
    [self setupUI];
}

#pragma mark - refreshView通知
- (void)refreshView:(NSNotification *)notification {
    NSDictionary *nameDictionary = [notification userInfo];
    _dataArr  = [nameDictionary objectForKey:@"name"];
    NSLog(@"%@",_dataArr);
    if (_dataArr.count > 0) {
        _tableView.hidden = NO;
        [_tableView reloadData];
    }else {
        _tableView.hidden = YES;
        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT/2-100, EMM_WIDTH, 20)];
        l.text = @"当前没有记录";
        l.textAlignment = NSTextAlignmentCenter;
        l.font = [UIFont systemFontOfSize:14];
        [self.view addSubview:l];
    }
}

- (void)setupUI {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 1)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#f7f7f7"];
    [self.view addSubview:imageView];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 1, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-51) style:UITableViewStylePlain];
    _tableView.height = _tableView.height - 50 - SafeAreaTopHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableView代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"NoticeCellId";
    NoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"NoticeCell" owner:self options:nil] lastObject];
        
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 89, EMM_WIDTH-20, 1)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [cell addSubview:imageView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //提醒类别：1、客户维护；2、客户关怀；3、员工关怀；4、认筹/认购/签约通知；5、审核结果
    if (_dataArr.count > 0) {
        _tableView.hidden = NO;
        NoticeList *list = [_dataArr objectAtIndex:indexPath.row];
        if ([list.isRead isEqualToString:@"已读"]) {
            cell.noticeYuanDianLabel.hidden = YES;
            cell.noticeNale1Label.text = list.noticeCategory;
        }else if ([list.isRead isEqualToString:@"未读"]) {
            cell.noticeYuanDianLabel.hidden = NO;
            cell.noticeYuanDianLabel .backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            cell.noticeNaleLabel.text = list.noticeCategory;

        }
        cell.noticeTimerLabel.text = list.noticeDate;
        cell.noticLabel.text = list.noticeTitle;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NoticeList *list = [_dataArr objectAtIndex:indexPath.row];
    DetailNoticeViewController *evc = [[DetailNoticeViewController alloc] init];
    evc.titleString = _titleStr;
    evc.noticeId = list.noticeId;
    [self.navigationController pushViewController:evc animated:YES];
}


@end
